package com.csdj.questions.controller;


import com.csdj.questions.common.WebResult;
import com.csdj.questions.common.WebResultMsgCode;
import com.csdj.questions.entity.AttentionQuestion;
import com.csdj.questions.entity.TQuestion;
import com.csdj.questions.entity.TQuestionTUsers;
import com.csdj.questions.service.TQuestionService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 问题表 前端控制器
 * </p>
 *
 * @author 刘宇
 * @since 2022-03-02
 */
@RestController
@RequestMapping("/t-question")
public class TQuestionController extends BaseController{
    @Resource
    private TQuestionService tQuestionService;

    /**
     * 连表查询 查user用户表和TQuestion评论表 等你来答里面的邀请回答
     * @name 刘宇
     * @return
     */
    @GetMapping ("/selectTQUAll")
    public WebResult selectTQUAll(){
        List<TQuestionTUsers> list = tQuestionService.selectTQuestionAll();
        if(list != null){
            System.out.println(list);
            return new WebResult(WebResultMsgCode.SUCCESS).putData("TQuestUsers",list);
        }
        return new WebResult(WebResultMsgCode.FAIL);
    }


    /**
     * 连表查询 查user用户表和TQuestion评论表 ，并且按照时间降序来排列  等你来答里面的最新问题
     * @name 刘宇
     * @return
     */
    @GetMapping("/selectTQUTimeDesc")
    public WebResult selectTQUTimeDesc(){
        List<TQuestionTUsers> list = tQuestionService.selectTQUTimeDesc();
        if(list != null){
            System.out.println(list);
            return new WebResult(WebResultMsgCode.SUCCESS).putData("TQuestUsersTimeDesc",list);
        }
        return new WebResult(WebResultMsgCode.FAIL);
    }

    /**
     * 连表查询 查user用户表和TQuestion评论表 并按照访问人数降序来排列  等你来答里面的人气问题
     * @name 刘宇
     * @return
     */
    @GetMapping("/selectTQUBrowseNumbDesc")
    public WebResult selectTQUBrowseNumbDesc() {
        List<TQuestionTUsers> list = tQuestionService.selectTQUBrowseNumbDesc();
        if(list != null){
            System.out.println(list);
            return new WebResult(WebResultMsgCode.SUCCESS).putData("selectTQUBrowseNumbDesc",list);
        }
        return new WebResult(WebResultMsgCode.FAIL);
    }

    /**
     * 用户发布问题
     * @name 郭成星
     * @param request 获取用户的id
     * @param QuerstionName 问题的名称
     * @param describe 问题的描述
     * @param topic 绑定的话题(最多5个)
     * @return
     */
    @PostMapping
    public  WebResult insertQuestion(HttpServletRequest request,
                                     @RequestParam(value = "image", required = false)String image,
                                     @RequestParam(value = "QuerstionName", required = true)String QuerstionName,
                                     @RequestParam(value = "describe", required = false)String describe,
                                     @RequestParam(value = "topic", required = true) String[] topic){
        int uid = getUid(request);
        StringBuffer stringBuffer=new StringBuffer();
        //如果话题数超过5个则不予执行发布问题
        if(!(topic.length<=5)){
            return new WebResult(WebResultMsgCode.FAIL).putData("topic",false);
        }

        for(String  append : topic){

            stringBuffer.append(append+",");
        }

        TQuestion tQuestion=new TQuestion();
        tQuestion.setUserId(uid);
        tQuestion.setQuesName(QuerstionName);
        tQuestion.setQuesDescribe(describe);
        //话题id
        tQuestion.setTopicId(stringBuffer.toString());
        //图片路径
        tQuestion.setPhoto(image);
       int jg= tQuestionService.insertQuestion(tQuestion);
        if(jg>0){

            return new WebResult(WebResultMsgCode.SUCCESS).putData("result",true);
        }
        return new WebResult(WebResultMsgCode.FAIL).putData("result",false);
    }

    /**
     * 根据问题id查询评论
     * @name 郭成星
     * @param QuestionId 问题id
     * @return
     */
    @GetMapping("/QuestionComment")
    public WebResult selQuestion(@RequestParam(value = "QuestionId", required = true)String QuestionId){
     List<TQuestionTUsers> tQuestionTUsersList=tQuestionService.selQuestion(QuestionId);

        if(tQuestionTUsersList!=null){

            return new WebResult(WebResultMsgCode.SUCCESS).putData("selQuestion",tQuestionTUsersList);
        }
            return  new WebResult(WebResultMsgCode.FAIL).putData("selQuestion",null);
    }

    /**
     *  多表查询，根据用户id查问题表的name
     * @name 刘宇
     * @return
     */
    @GetMapping("/selectName")
    public WebResult selectName(HttpServletRequest request){
        int uid = getUid(request);
        List<AttentionQuestion> list = tQuestionService.selectName(uid);
        if(list != null){
            //返回一个0 代表成功
            return new WebResult(WebResultMsgCode.SUCCESS).putData("",list);
        }
        //返回1 代表失败
        return new WebResult(WebResultMsgCode.FAIL);
    }
}
